package es.valma.VideoClub.intefaces.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.valma.VideoClub.DAO.base.VideoClubSvClienteDao;
import es.valma.VideoClub.base.VideoClubSvCliente;
import es.valma.VideoClub.exception.DAO.base.VideoClubClienteDaoException;

public class LoginWebController implements IWebController{

	private VideoClubSvClienteDao clienteDao;
	
	
	public LoginWebController(){
		try{
			clienteDao=new VideoClubSvClienteDao();
		}catch(Throwable a){
			System.out.println(a);
		}
	}
	
	
	
	@Override
	public String doProcess(HttpServletRequest request,
			HttpServletResponse response) {
		
		String view=null;
	    
	    String login=request.getParameter("login");
	    String password=request.getParameter("password");
	    
	    try{
	    	VideoClubSvCliente clienteActual = obtCliente(login,password);
	        //metemos los resultados a mostrar en la vista dentro de la req.
	        //request.setAttribute("clienteActual", clienteActual);
	    	if(clienteActual!=null){
		        HttpSession sesion=request.getSession();
		        sesion.setAttribute("clienteActual", clienteActual);
		        view = "/principal";
	    	}else{
	    		view = "/index";
	    	}
	      }catch(VideoClubClienteDaoException e){
	        /*if(e.getMessage().equals("ClientDao. Error User no existente")){
	          request.setAttribute("login", login);
	          request.setAttribute("password", password);
	          view="/registro.jsp";
	        }else if(e.getMessage().equals("ClientDao. Error en el SQL compruebaLogin")){
	          view="/error.jsp";
	        }
	        else{*/
	         view="/index";
	       // }
	      }
	      return view;
	}
	
	
	private VideoClubSvCliente obtCliente(String login,String password) {
	    //Proceso de login
		VideoClubSvCliente clienteActual=clienteDao.compruebaLogin(login, password);
	    return clienteActual;
	  }
	  
	

}
